<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--企业数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="小区名称" prop="userName">
              <el-input v-model="queryParams.userName" placeholder="请输入小区名称" clearable style="width: 240px"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="营业点" prop="status">
              <el-select v-model="queryParams.nickName" filterable placeholder="请选择">
                <el-option label="营业点一" value="1"> </el-option>
                <el-option label="营业点二" value="2"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="手机号" prop="userName">
              <el-input v-model="queryParams.userName" type="number" placeholder="请输入手机号" clearable style="width: 240px"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['fileManagement:civilArchives:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                v-hasPermi="['fileManagement:civilArchives:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['fileManagement:civilArchives:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                v-hasPermi="['fileManagement:civilArchives:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['fileManagement:civilArchives:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="userList" border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="单位名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="区域名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="小区名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="营业点" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="负责人" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="联系电话" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="余额提醒阈值" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" width="100" />
            <el-table-column label="短信提醒号码" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" width="100" />
            <el-table-column label="欠费关阀" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="缴费开阀" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="收费月份" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="账单日" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="自动发送短信" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" width="100" />
            <el-table-column label="账户余额" align="center" key="deptName" prop="dept.deptName"
              :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['fileManagement:civilArchives:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['fileManagement:civilArchives:remove']">删除</el-button>
                <el-button size="mini" type="text" icon="el-icon-tickets" @click="handlAaddBj(scope.row)"
                  v-hasPermi="['fileManagement:civilArchives:addBj']">新增表计</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

      </el-col>
    </el-row>

    <!-- 添加或修改小区配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-collapse v-model="activeNames" accordion>
        <el-collapse-item title="基本信息" name="1">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="客户ID" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户卡号" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单位名称" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入单位名称" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="营业点" prop="nickName">
                  <el-select v-model="form.nickName" filterable placeholder="请选择">
                    <el-option label="营业点一" value="1"> </el-option>
                    <el-option label="营业点二" value="2"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="区域名称" prop="nickName">
                  <el-select v-model="form.nickName" filterable placeholder="请选择">
                    <el-option label="名称一" value="1"> </el-option>
                    <el-option label="名称二" value="2"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="小区名称" prop="nickName">
                  <el-select v-model="form.nickName" filterable placeholder="请选择">
                    <el-option label="名称一" value="1"> </el-option>
                    <el-option label="名称二" value="2"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="负责人" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入负责人" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话" prop="nickName">
                  <el-input v-model="form.nickName" type="number" placeholder="请输入联系电话" maxlength="11" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="身份证号" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入身份证号" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="地址(车间)" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入地址(车间)" maxlength="30" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-collapse-item>
        <el-collapse-item title="计价信息" name="2">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="余额提醒阈值" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入余额提醒阈值" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="短信提醒号码" prop="nickName">
                  <el-input v-model="form.nickName" placeholder="请输入短信提醒号码" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="欠费关阀" prop="nickName">
                  <el-radio-group v-model="form.radio">
                    <el-radio :label="3">否</el-radio>
                    <el-radio :label="6">是</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="缴费开阀" prop="nickName">
                  <el-radio-group v-model="form.radio">
                    <el-radio :label="3">否</el-radio>
                    <el-radio :label="6">是</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="收费月份" prop="nickName">
                  <el-select v-model="form.nickName" filterable placeholder="请选择">
                    <el-option label="每月" value="1"> </el-option>
                    <el-option label="单月" value="2"> </el-option>
                    <el-option label="双月" value="3"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="账单日" prop="nickName">
                  <el-select v-model="form.nickName" filterable placeholder="请选择">
                    <el-option label="1" value="1"> </el-option>
                    <el-option label="2" value="2"> </el-option>
                    <el-option label="3" value="3"> </el-option>
                    <el-option label="4" value="4"> </el-option>
                    <el-option label="5" value="5"> </el-option>
                    <el-option label="6" value="6"> </el-option>
                    <el-option label="7" value="7"> </el-option>
                    <el-option label="8" value="8"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="自动发送短信" prop="nickName">
                  <el-radio-group v-model="form.radio">
                    <el-radio :label="1">否</el-radio>
                    <el-radio :label="2">是</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-collapse-item>
      </el-collapse>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增表计弹框 -->
    <el-dialog :title="title2" :visible.sync="bjOPen" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="基表表号" prop="deviceId">
              <el-input v-model="form.deviceId" placeholder="请输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="气表品牌" prop="meterBrand">
              <el-select v-model="form.meterBrand" filterable placeholder="请选择">
                <el-option label="类型一" value="1"> </el-option>
                <el-option label="类型二" value="2"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基表厂家" prop="meterManufacturer">
              <el-select v-model="form.meterManufacturer" filterable placeholder="请选择">
                <el-option label="类型一" value="1"> </el-option>
                <el-option label="类型二" value="2"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用气类型" prop="meterType">
              <el-select v-model="form.meterType" filterable placeholder="请选择">
                <el-option label="工用" value="1"> </el-option>
                <el-option label="商用" value="2"> </el-option>
                <el-option label="民用" value="3"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价" prop="nickName">
              <el-input v-model="form.nickName" type="number" placeholder="请输入单价" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户类型" prop="meterModel">
              <el-select v-model="form.meterModel" filterable placeholder="请选择">
                <el-option label="类型一" value="1"> </el-option>
                <el-option label="类型二" value="2"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="DTU/RTU编号" prop="dtuCode">
              <el-input v-model="form.dtuCode" placeholder="请输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基表对应ICCID" prop="iccid">
              <el-input v-model="form.iccid" placeholder="请输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流量计起始量" prop="initialGas">
              <el-input v-model="form.initialGas" placeholder="请输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="进气方向" prop="intakeDirection">
              <el-radio-group v-model="form.intakeDirection">
                <el-radio label="L">左进</el-radio>
                <el-radio label="R">右进</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="同步到IOT平台" prop="nickName">
              <el-select v-model="form.nickName" filterable placeholder="请选择">
                <el-option label="否" value="N"> </el-option>
                <el-option label="是" value="Y"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否带GPRS" prop="isGprs">
              <el-select v-model="form.isGprs" filterable placeholder="请选择">
                <el-option label="否" value="N"> </el-option>
                <el-option label="是" value="Y"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否带流量计" prop="isFlowmeter">
              <el-select v-model="form.isFlowmeter" filterable placeholder="请选择">
                <el-option label="否" value="N"> </el-option>
                <el-option label="是" value="Y"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流量计厂家" prop="flowmeterManufacturer">
              <el-input v-model="form.flowmeterManufacturer" placeholder="请输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="表计状态" prop="meterStatus">
              <el-select v-model="form.meterStatus" filterable placeholder="请选择">
                <el-option label="待安装" :value="1"> </el-option>
                <el-option label="正常" :value="2"> </el-option>
                <el-option label="已拆卸" :value="3"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安装日期" prop="installTime">
              <el-date-picker v-model="form.installTime" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  deptTreeSelect,
} from "@/api/system/user";
import { getToken } from "@/utils/auth";

export default {
  name: "IndustrialArchives",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 企业树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 企业名称
      deptName: undefined,
      //是否表计显示弹出层
      bjOPen: false,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "小区名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "小区名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        nickName: [
          { required: true, message: "小区昵称不能为空", trigger: "blur" },
        ],
      },
      activeNames: ["1"],
    };
  },
  watch: {
    // 根据名称筛选企业树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询小区列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams)).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询企业下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表计取消按钮
    cancel2() {
      this.bjOPen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUser().then((response) => {
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "添加民用档案";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getUser(userId).then((response) => {
        this.form = response.data;
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.$set(this.form, "postIds", response.postIds);
        this.$set(this.form, "roleIds", response.roleIds);
        this.open = true;
        this.title = "修改民用档案";
        this.form.password = "";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //表计提交按钮
    submitForm2() {
      this.bjOPen = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal
        .confirm('是否确认删除小区编号为"' + userIds + '"的数据项？')
        .then(function () {
          return delUser(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handlAaddBj(row) {
      this.bjOPen = true;
      this.title2 = "添加表计";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "小区导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "system/user/importTemplate",
        {},
        `user_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss" scoped>
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}
</style>
